package john.wenzi.server.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AESUtil {
	/**
     * 随机生成AES密钥
     */
	public static String getGenerateKey()throws Exception{
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] encoded = secretKey.getEncoded();
        return Base64.encodeBase64String(encoded);
    }
	
	/**
     * 将String类型的密钥转化为SecretKey类型
     */
    private static SecretKey StringToSecretKey(String generateKey){
        byte[] bytes = Base64.decodeBase64(generateKey);
        SecretKeySpec spec = new SecretKeySpec(bytes,"AES");
        return  spec;
    }
    
    /**
     * 文件加密
     */
    public static File encrypt(File sourceFile,String fileType, String generateKey)throws Exception{
        SecretKey secretKey = StringToSecretKey(generateKey);
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        
        File encrypfile = null;  
        InputStream inputStream = null;  
        OutputStream outputStream = null;  
        try {  
            inputStream = new FileInputStream(sourceFile);  
            encrypfile = File.createTempFile(sourceFile.getName()+"1", fileType);  
            outputStream = new FileOutputStream(encrypfile);  
            //以加密流写入文件  
            CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);  
            byte[] cache = new byte[1024];  
            int nRead = 0;  
            while ((nRead = cipherInputStream.read(cache)) != -1) {  
                outputStream.write(cache, 0, nRead);  
                outputStream.flush();  
            }  
            cipherInputStream.close();  
        }  catch (FileNotFoundException e) {  
            e.printStackTrace(); 
        }  catch (IOException e) {  
            e.printStackTrace(); 
        } finally {  
            try {  
                inputStream.close();  
                outputStream.close(); 
            } catch (IOException e) {  
                e.printStackTrace();
            }  
        }  
        return encrypfile; 
    }
    /**
     * 文件解密
     */
    public static File decryptFile(File sourceFile,String fileType,String generateKey)throws Exception{ 
		SecretKey secretKey = StringToSecretKey(generateKey);
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        
        File encrypfile = null;  
        InputStream inputStream = null;  
        OutputStream outputStream = null;  
        try {  
            inputStream = new FileInputStream(sourceFile);  
            encrypfile = File.createTempFile(sourceFile.getName()+"1", fileType);  
            outputStream = new FileOutputStream(encrypfile);  
            //以加密流写入文件  
            CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);  
            byte[] cache = new byte[1024];  
            int nRead = 0;  
            while ((nRead = cipherInputStream.read(cache)) != -1) {  
                outputStream.write(cache, 0, nRead);  
                outputStream.flush();  
            }  
            cipherInputStream.close();  
        }  catch (FileNotFoundException e) {  
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
        }  catch (IOException e) {  
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
        } finally {  
            try {  
                inputStream.close();  
            } catch (IOException e) {  
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
            }  
            try {  
                outputStream.close();  
            } catch (IOException e) {  
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
            }  
        }  
        return encrypfile; 
    }
}
